package ysq.xyg.base.util;

import javax.servlet.http.HttpServletRequest;
import java.io.File;


/**
 * 附件处理方法
 *
 * @author shengQiang yu
 * @date 2019-03-30 14:06
 */
public class FileUtil {

    /**
     * 单个文件删除(如果附件存在就删除，不存在默认为已删除）
     *
     * @param request HttpServletRequest对象
     * @param path 文件相对路径
     * @return
     * @author shengQiang yu
     * @date 2019-03-07 16:14
     */
    public static void deleteFile(HttpServletRequest request,String path){
        File file=new File(acceptRealPath(request)+path);
        if(file.isFile() &&file.exists()){
            file.delete();
        }
    }

    /**
     * 判断当前文件是否存在
     *
     * @param request HttpServletRequest对象
     * @param path 文件相对路径
     * @return
     * @author shengQiang yu
     * @date 2019-03-07 16:11
     */
    public static boolean checkFile(HttpServletRequest request,String path){
        File file=new File(acceptRealPath(request)+path);
        if(file.exists()){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 获取相对路径
     *
     * @param request HttpServletRequest对象
     * @return
     * @author shengQiang yu
     * @date 2019-03-07 16:09
     */
    public static String acceptRealPath(HttpServletRequest request){
        return ((HttpServletRequest) request).getServletContext().getRealPath("/");
    }
}
